package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsCartMapper;
import com.qiguliuxing.dts.db.domain.DtsCart;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsCartService {
	@Resource
	private DtsCartMapper cartMapper;

	public DtsCart queryExist(Integer goodsId, Integer productId, Integer userId) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsCart::getGoodsId, goodsId);
		queryWrapper.lambda().eq(DtsCart::getProductId, productId);
		queryWrapper.lambda().eq(DtsCart::getUserId, userId);
		queryWrapper.eq("deleted", false);
		return cartMapper.selectOne(queryWrapper);
	}

	public void add(DtsCart cart) {
		cart.setAddTime(LocalDateTime.now());
		cart.setUpdateTime(LocalDateTime.now());
		cartMapper.insert(cart);
	}

	public int updateById(DtsCart cart) {
		cart.setUpdateTime(LocalDateTime.now());
		return cartMapper.updateById(cart);
	}

	public List<DtsCart> queryByUid(int userId) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsCart::getUserId, userId);
		queryWrapper.eq("deleted", false);
		return cartMapper.selectList(queryWrapper);
	}

	public List<DtsCart> queryByUidAndChecked(Integer userId) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsCart::getUserId, userId);
		queryWrapper.eq("checked", true);
		queryWrapper.eq("deleted", false);
		return cartMapper.selectList(queryWrapper);
	}

	public int delete(List<Integer> productIdList, int userId) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsCart::getUserId, userId);
		queryWrapper.lambda().in(DtsCart::getProductId, productIdList);
		return cartMapper.delete(queryWrapper);
	}

	public DtsCart findById(Integer id) {
		return cartMapper.selectById(id);
	}

	public int updateCheck(Integer userId, List<Integer> idsList, Boolean checked) {
		UpdateWrapper<DtsCart> updateWrapper = new UpdateWrapper<>();
		updateWrapper.lambda().eq(DtsCart::getUserId, userId);
		updateWrapper.lambda().in(DtsCart::getProductId, idsList);
		updateWrapper.eq("deleted", false);
		DtsCart cart = new DtsCart();
		cart.setChecked(checked);
		cart.setUpdateTime(LocalDateTime.now());
		return cartMapper.update(cart, updateWrapper);
	}

	public void clearGoods(Integer userId) {
		UpdateWrapper<DtsCart> updateWrapper = new UpdateWrapper<>();
		updateWrapper.lambda().eq(DtsCart::getUserId, userId);
		updateWrapper.eq("checked", true);
		DtsCart cart = new DtsCart();
		cart.setDeleted(true);
		cartMapper.update(cart, updateWrapper);
	}

	public List<DtsCart> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort,
			String order) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		if (userId != null) {
			queryWrapper.lambda().eq(DtsCart::getUserId, userId);
		}
		if (goodsId != null) {
			queryWrapper.lambda().eq(DtsCart::getGoodsId, goodsId);
		}
		queryWrapper.eq("deleted", false);

		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}

		IPage<DtsCart> iPage = new Page<>(page, limit);
		iPage = cartMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}

	public void deleteById(Integer id) {
		cartMapper.deleteById(id);
	}

	public boolean checkExist(Integer goodsId) {
		QueryWrapper<DtsCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsCart::getGoodsId, goodsId);
		queryWrapper.eq("checked", true);
		queryWrapper.eq("deleted", false);
		return cartMapper.selectCount(queryWrapper) != 0;
	}
}
